<?php

class FWidget
{
    private $xtype;    
    private $id;    
    private $name;    
    private $table;    
    private $hint;    
    private $fieldLabel;
    private $labelWidth;
    private $fieldWidth;    
    private $className;    
    private $readOnly;    
    private $required;
    private $hidden;
    private $region;    
    private $baseCls;
    private $fieldCls;
    private $labelCls;
    private $readOnlyCls;
    private $requiredCls;
    private $height;    
    private $width;    
    private $minHeight;    
    private $minWidth;    
    private $layout;
    private $layoutConfig;
    private $items;    
    private $text;
    private $value;
    private $cls;    
    private $padding;    
    private $flex;    
    private $x;    
    private $y;    
    private $html;
    private $renderTo;
    private $grow;
    private $rows;
    private $maxLength;
    private $handler;
    private $inputType;
    private $msg;
    private $title;
    
    private $marginTop = 0;
    private $marginRight = 0;
    private $marginBottom = 0;
    private $marginLeft = 0;
    
    private $config;
    
    private $onClick;
    
    private $clickFunction;
    private $dblClickFunction;
    private $onKeyDown = array();
    
    private $isInput = false;
    
    private $formAction;
    
    const INPUT_TYPE_PASSWORD = 'password';
    
    const MAX_LENGTH_TEXT = 'Há mais caracteres do que o limite deste campo.';
    
    public function __construct($name, $xtype = 'ftextfield', $fieldLabel = '', $fieldWidth = 20, $readOnly = false)
    {
        $this->id = $name;
        $this->name = $name;
        $this->xtype = $xtype;
        $this->fieldLabel = $fieldLabel;
        $this->setFieldWidth($fieldWidth);
        $this->readOnly = $readOnly;
    }
    
    public function generate()
    {
        $allowBlank = $this->getRequired() ? 'false' : 'true';
        $readOnly = $this->getReadOnly() ? 'true' : 'false';
        $hidden = $this->getHidden() ? 'true' : 'false';
        $formAction = $this->getFormAction();
        
        $jsCode = " 
        {
            xtype: '{$this->getXtype()}',";
            
        if ( !$this->getIsInput() )
        {
            $jsCode .= "id: '{$this->getId()}',";
        }
                    
        $jsCode .= "
            name: '{$this->getName()}',
            allowBlank: $allowBlank,
            readOnly: $readOnly,
            hidden: $hidden, 
            enableKeyEvents: true,
            formAction: '$formAction',
            listeners: {
                click: {
                    element: 'el',
                    fn: function() {
                        {$this->clickFunction};
                    }
                },
                dblclick: {
                    element: 'el',
                    fn: function() {
                        {$this->dblClickFunction};
                    }
                },
                keydown: {
                    element: 'el',
                    fn: function(e, el) {";
                        
        foreach( $this->onKeyDown as $keyCode => $keyDown )
        {
            $jsCode .= "
                        if ( e.getCharCode() == {$keyCode} )
                        {
            ";
                        
            if ( $keyDown['object'] )
            {
                $jsCode .= "                    
                    fform_{$keyDown['object']}.{$keyDown['action']};};";
            }
            else
            {
                $jsCode .= "{$keyDown['action']};};";
            }
        }

        $jsCode .= "}},";
        
        // Fim dos listeners
        $jsCode .= "},";
        
        if ( $this->getHandler() )
        {
            $jsCode .= " handler: {$this->getHandler()},
                scope: this,
            ";
        }
        
        if (strlen($this->getInputType()) > 0 )
        {
            $jsCode .= " inputType: '{$this->getInputType()}', ";
        }
            
        if ( strlen($this->getFieldLabel()) > 0 )
        {
            $jsCode .= " fieldLabel: '{$this->getFieldLabel()}', ";
        }
        
        if ( $this->getLabelWidth() )
        {
            $jsCode .= " labelWidth: {$this->getLabelWidth()}, ";
        }

        if ( strlen($this->getRegion()) > 0 )
        {
            $jsCode .= " region: '{$this->getRegion()}', ";
        }
        
        if ( strlen($this->getBaseCls()) > 0 )
        {
            $jsCode .= " baseCls: '{$this->getBaseCls()}', ";
        }
        
        if ( strlen($this->getFieldCls()) > 0 )
        {
            $jsCode .= " fieldCls: '{$this->getFieldCls()}', ";
        }
        
        if ( strlen($this->getLabelCls()) > 0 )
        {
            $jsCode .= " labelCls: '{$this->getLabelCls()}', ";
        }
        
        if ( strlen($this->getReadOnlyCls()) > 0 )
        {
            $jsCode .= " readOnlyCls: '{$this->getReadOnlyCls()}', ";
        }
        
        if ( strlen($this->getRequiredCls()) > 0 )
        {
            $jsCode .= " requiredCls: '{$this->getRequiredCls()}', ";
        }
        
        if ( $this->getHeight() )
        {
            if ( is_int($this->getHeight()) )
            {
                $jsCode .= " height: {$this->getHeight()}, ";
            }
            else
            {
                $jsCode .= " height: '{$this->getHeight()}', ";
            }
        }
        
        if ( $this->getMinHeight() )
        {
            $jsCode .= " minHeight: {$this->getMinHeight()}, ";
        }
        
        if ( $this->getWidth() )
        {
            if ( is_int($this->getWidth()) )
            {
                $jsCode .= " width: {$this->getWidth()}, ";
            }
            else
            {
                $jsCode .= " width: '{$this->getWidth()}', ";
            }
        }
        else
        {
            if ( $this->getFieldWidth() )
            {
                $jsCode .= " width: {$this->getFieldWidth()}, ";
            }
        }
        
        if ( $this->getMinWidth() )
        {
            $jsCode .= " minWidth: {$this->getMinWidth()}, ";
        }
        
        if ( strlen($this->getMsg()) > 0 )
        {
            $jsCode .= " msg: '{$this->getMsg()}', ";
        }
        
        if ( strlen($this->getTitle()) > 0 )
        {
            $jsCode .= " title: '{$this->getTitle()}', ";
        }
        
        if ( $this->getMaxLength() )
        {
            $maxLengthText = self::MAX_LENGTH_TEXT;
            
            $jsCode .= " maxLength: {$this->getMaxLength()}, 
                         maxLengthText: '{$maxLengthText}',
                         enforceMaxLength: true, ";
        }
        
        if ( strlen($this->getLayout()) > 0 )
        {
            $layoutConfig = $this->getLayoutConfig();
            if ( is_array($layoutConfig) )
            {
                $jsCode .= " layout: { ";

                $jsCode .= "type: '{$this->getLayout()}', ";
                foreach ( $layoutConfig as $key => $lConf )
                {
                    $jsCode .= "$key: $lConf, ";
                }

                $jsCode .= " },";
            }
            else
            {
                $jsCode .= " layout: '{$this->getLayout()}', ";
            }
        }
        
        if ( strlen($this->getValue()) > 0 )
        {
            $jsCode .= " value: '{$this->getValue()}', ";
        }
        
        if ( strlen($this->getText()) > 0 )
        {
            $jsCode .= " text: '{$this->getText()}', ";
        }
        
        if ( strlen($this->getCls()) > 0 )
        {
            $jsCode .= " cls: '{$this->getCls()}', ";
        }
        
        if ( strlen($this->getPadding()) > 0 )
        {
            $jsCode .= " padding: '{$this->getPadding()}', ";
        }
        
        if ( $this->marginTop || $this->marginRight || $this->marginBottom || $this->marginLeft )
        {
            $jsCode .= " margin: '{$this->marginTop} {$this->marginRight} {$this->marginBottom} {$this->marginLeft}', ";
        }
        
        if ( $this->getFlex() > 0 )
        {
            $jsCode .= " flex: {$this->getFlex()}, ";
        }
        
        if ( $this->getX() > 0 )
        {
            $jsCode .= " x: {$this->getX()}, ";
        }
        
        if ( $this->getY() > 0 )
        {
            $jsCode .= " y: {$this->getY()}, ";
        }
        
        if ( strlen($this->getHtml()) > 0 )
        {
            $jsCode .= " html: '{$this->getHtml()}', ";
        }
        
        if ( strlen($this->getRenderTo()) > 0 )
        {
            $jsCode .= " renderTo: {$this->getRenderTo()}, ";
        }
        
        if ( $this->getGrow() )
        {
            $jsCode .= ' grow: true, ';
        }
        
        if ( is_int($this->getRows()) )
        {
            $jsCode .= " rows: {$this->getRows()}, ";
        }
        
        foreach ( $this->getConfig() as $config )
        {
            $jsCode .= " {$config['key']}: '{$config['value']}', ";
        }
        
        // Items
        $jsCode .= ' items: [ ';
        foreach ( $this->getItems() as $item )
        {
            if ( $item instanceof FWidget )
            {
                $jsCode .= $item->generate() . ',';
            }
        }
        $jsCode .= ' ],';
                
        $jsCode .= " } ";
                       
        return $jsCode;
    }
    
    public function getXtype() {
        return $this->xtype;
    }

    public function setXtype($xtype) {
        $this->xtype = $xtype;
    }
    
    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getHint() {
        return $this->hint;
    }

    public function setHint($hint) {
        $this->hint = $hint;
    }
    
    public function getFieldLabel() {
        return $this->fieldLabel;
    }

    public function setFieldLabel($fieldLabel) {
        $this->fieldLabel = $fieldLabel;
    }
    
    public function getLabelWidth() {
        return $this->labelWidth;
    }

    public function setLabelWidth($labelWidth) {
        $this->labelWidth = $labelWidth;
    }
        
    public function getFieldWidth() {
        return $this->fieldWidth;
    }

    public function setFieldWidth($fieldWidth) {
        $this->fieldWidth = $fieldWidth + 300;
    }
    
    public function getReadOnly() {
        return $this->readOnly;
    }

    public function setReadOnly($readOnly) {
        $this->readOnly = $readOnly;
    }

    public function getRequired() {
        return $this->required;
    }

    public function setRequired($required) {
        $this->required = $required;
    }
    
    public function getClassName()
    {
        if ( strlen($this->className) > 0 )
        {
            return $this->className;
        }
        else
        {
            return 'FDefaultModel';
        }
    }

    public function setClassName($className) {
        $this->className = $className;
    }
    
    public function getTable() {
        return $this->table;
    }

    public function setTable($table) {
        $this->table = $table;
    }
    
    public function getRegion() {
        return $this->region;
    }

    public function setRegion($region) {
        $this->region = $region;
    }

    public function getBaseCls() {
        return $this->baseCls;
    }

    public function setBaseCls($baseCls) {
        $this->baseCls = $baseCls;
    }
    
    public function getFieldCls() {
        return $this->fieldCls;
    }

    public function setFieldCls($fieldCls) {
        $this->fieldCls = $fieldCls;
    }

    public function getLabelCls() {
        return $this->labelCls;
    }

    public function setLabelCls($labelCls) {
        $this->labelCls = $labelCls;
    }

    public function getReadOnlyCls() {
        return $this->readOnlyCls;
    }

    public function setReadOnlyCls($readOnlyCls) {
        $this->readOnlyCls = $readOnlyCls;
    }

    public function getRequiredCls() {
        return $this->requiredCls;
    }

    public function setRequiredCls($requiredCls) {
        $this->requiredCls = $requiredCls;
    }
        
    public function getHeight() {
        return $this->height;
    }

    public function setHeight($height) {
        $this->height = $height;
    }

    public function getMinHeight() {
        return $this->minHeight;
    }

    public function setMinHeight($minHeight) {
        $this->minHeight = $minHeight;
    }
    
    public function getWidth() {
        return $this->width;
    }

    public function setWidth($width) {
        $this->width = $width;
    }

    public function getMinWidth() {
        return $this->minWidth;
    }

    public function setMinWidth($minWidth) {
        $this->minWidth = $minWidth;
    }
    
    public function getLayout() {
        return $this->layout;
    }

    public function setLayout($layout) {
        $this->layout = $layout;
    }
    
    public function getLayoutConfig() {
        return $this->layoutConfig;
    }

    public function setLayoutConfig($layoutConfig) {
        $this->layoutConfig = $layoutConfig;
    }
        
    public function getItems() {
        return $this->items;
    }

    public function addItem($item)
    {
        $this->items[] = $item;
    }
    
    public function getValue() {
        return $this->value;
    }

    public function setValue($value) {
        $this->value = $value;
    }

    public function getText() {
        return $this->text;
    }

    public function setText($text) {
        $this->text = $text;
    }
    
    public function getCls() {
        return $this->cls;
    }

    public function setCls($cls) {
        $this->cls = $cls;
    }
    
    public function getPadding() {
        return $this->padding;
    }

    public function setPadding($padding) {
        $this->padding = $padding;
    }
    
    public function getFlex() {
        return $this->flex;
    }

    public function setFlex($flex) {
        $this->flex = $flex;
    }
    
    public function getX() {
        return $this->x;
    }

    public function setX($x) {
        $this->x = $x;
    }

    public function getY() {
        return $this->y;
    }

    public function setY($y) {
        $this->y = $y;
    }
    
    public function getHtml() {
        return $this->html;
    }

    public function setHtml($html) {
        $this->html = $html;
    }
    
    public function getConfig() {
        return $this->config;
    }
    
    public function addConfig($config)
    {
        $this->config[] = $config;
    }
    
    public function getOnClick() {
        return $this->onClick;
    }

    public function setOnClick($onClick) {
        $this->onClick = $onClick;
    }
    
    public function getIsInput() {
        return $this->isInput;
    }

    public function setIsInput($isInput) {
        $this->isInput = $isInput;
    }
    
    public function getRenderTo() {
        return $this->renderTo;
    }

    public function setRenderTo($renderTo) {
        $this->renderTo = $renderTo;
    }
    
    public function getHidden() {
        return $this->hidden;
    }

    public function setHidden($hidden) {
        $this->hidden = $hidden;
    }
    
    public function getGrow() {
        return $this->grow;
    }
    
    // atributos da classe FTextArea
    public function setGrow($grow) {
        $this->grow = $grow;
    }
    
    public function getRows() {
        return $this->rows;
    }

    public function setRows($rows) {
        $this->rows = $rows;
    }
    
    public function getMaxLength() {
        return $this->maxLength;
    }

    public function setMaxLength($maxLength) {
        $this->maxLength = $maxLength;
    }
    
    public function getHandler() {
        return $this->handler;
    }

    public function setHandler($handler) {
        $this->handler = $handler;
    }
        
    // fim dos atributos da classe FTextArea
    
    public function getClickFunction() {
        return $this->clickFunction;
    }

    public function setClickFunction($clickFunction) {
        $this->clickFunction = $clickFunction;
    }

    public function getDblClickFuncion() {
        return $this->dblClickFunction;
    }

    public function setDblClickFunction($dblClickFuncion) {
        $this->dblClickFunction = $dblClickFuncion;
    }
    
    public function getOnKeyDown() {
        return $this->onKeyDown;
    }

    public function setOnKeyDown($keyNumber, $action, $object = NULL)
    {
        $this->onKeyDown[$keyNumber]['action'] = $action;
        $this->onKeyDown[$keyNumber]['object'] = $object;
    }
    
    public function getInputType() {
        return $this->inputType;
    }

    public function setInputType($inputType) {
        $this->inputType = $inputType;
    }
    
    public function getMsg() {
        return $this->msg;
    }

    public function setMsg($msg) {
        $this->msg = $msg;
    }

    public function getTitle() {
        return $this->title;
    }

    public function setTitle($title) {
        $this->title = $title;
    }
    
    public function getMarginTop() {
        return $this->marginTop;
    }

    public function setMarginTop($marginTop) {
        $this->marginTop = $marginTop;
    }

    public function getMarginRight() {
        return $this->marginRight;
    }

    public function setMarginRight($marginRight) {
        $this->marginRight = $marginRight;
    }

    public function getMarginBottom() {
        return $this->marginBottom;
    }

    public function setMarginBottom($marginBottom) {
        $this->marginBottom = $marginBottom;
    }

    public function getMarginLeft() {
        return $this->marginLeft;
    }

    public function setMarginLeft($marginLeft) {
        $this->marginLeft = $marginLeft;
    }
    
    public function getFormAction()
    {
        if ( !$this->formAction )
        {
            $this->formAction = FEditForm::FUNCTION_INSERT;
        }
        
        return $this->formAction;
    }

    public function setFormAction($formAction) {
        $this->formAction = $formAction;
    }
                
    public function addEvents()
    {
        $jsCode = " var {$this->getName()} = Ext.get(\"{$this->getId()}\"); ";
        
        $onClick = $this->getOnClick();
        if ( $onClick )
        {            
            if ( $onClick instanceof FForm )
            {
                $onClick->generate();
                $onClick = $onClick->show();
            }
            
            $jsCode .= "
                function {$this->getName()}onClick() {
                    $onClick;
                };
               
                {$this->getName()}.on(\"click\", {$this->getName()}onClick);
            ";
        }
        
        return $jsCode;
    }

}

?>
